package com.pason.onlineshop.mainshop.service.task.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pason.onlineshop.mainshop.domain.Order;
import com.pason.onlineshop.mainshop.service.entity.IOrderEntityService;
import com.pason.onlineshop.mainshop.service.entity.query.impl.QueryCondition;
import com.pason.onlineshop.mainshop.service.entity.query.impl.QueryParameter;
import com.pason.onlineshop.mainshop.service.task.IShopQueryTaskService;

@Service
public class ShopQueryTaskServiceImpl implements IShopQueryTaskService {
	private Log log = LogFactory.getLog(ShopQueryTaskServiceImpl.class);	
	
	@Autowired(required=true)
	IOrderEntityService orderEntityService;
	
	@Override
	public List<Order> queryOrders(final String jsonParams) {
		
		QueryCondition queryCondition = new QueryCondition();
		queryCondition.setModelName("Order");
		
		try{
			Map<String, List<String>> paramMap = new ObjectMapper().readValue(jsonParams, new TypeReference<Map<String,List<String>>>(){});
			Iterator<String> itr = paramMap.keySet().iterator();
			while(itr.hasNext()) {
				String key = itr.next();
				QueryFieldResult qfr = QueryUtil.getQueryFieldByWebName(Order.class, key);
				if(null != qfr) {
				    QueryParameter param = new QueryParameter(qfr.getFieldDBName(), paramMap.get(key), qfr.getQueryOperateType());
				    queryCondition.add(param);
				}
			}
			
			return orderEntityService.searchOrders(queryCondition);

		} catch (Exception e) {
			e.printStackTrace();
			log.error("parse queryOrders jsonParams error");
		}
		
		return null;
	}

}
